package Level02;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

/**
 * Map 中有{张三=23,李四=24,王五=15,赵六=36,孙七=17,田七=57,周八=35}这些元素 key 为姓名
 * value 为年龄.将周八的年龄修改为 22,并将年龄小于 30 岁人的信息写入到 perosn.txt
 * 中,person.txt 每行数据格式为:姓名=年龄
 */

public class Thirteen {
    public static void main(String[] args) {
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("张三", 23);
        hashMap.put("李四", 24);
        hashMap.put("王五", 15);
        hashMap.put("赵六", 36);
        hashMap.put("田七", 17);
        hashMap.put("周八", 35);
        hashMap.put("周八", 22);
        System.out.println("修改后：周八的年龄为：" + hashMap.get("周八"));
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File("person.txt"), true));
            Set<String> keySet = hashMap.keySet();
            for (String str : keySet) {
                Integer age = hashMap.get(str);
                if (age < 30) {
                    bufferedWriter.write(str + "," + hashMap.get(str));
                    bufferedWriter.flush();
                    bufferedWriter.newLine();//换行
                }
            }
            System.out.println("数据已经成功写到person.txt");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

